"use strict";

$(function () {
    "use strict";
    // 判断登录状态

    if ($.cookie("isLogin")) {
        $(".header-user").removeClass("icon-user");
        var img = $('<img src="" alt="">');
        img.attr("src", "../assets/images/user.jpg");
        $(".header-user").prepend(img);

        var user = $(".header-user");
        var user_list = user.find(".YesLogin");
        user.hover(function () {
            user_list.stop().slideDown();
        }, function () {
            user_list.stop().slideUp();
        });
        $(".out").click(function () {
            $.cookie("isLogin", null);
            window.location.href = "../login.html";
        });
    }
    // 如果购物车中有商品，将数字改变
    if ($.cookie("NUM")) {
        $(".header-cart em").html($.cookie("NUM"));
    }
    // 首页开始
    var main = $(".main");
    // 数据请求
    $.ajax({
        url: "../data/MeizuCont.json",
        type: "get",
        success: function success(res) {
            // console.log(res)
            var $title1 = "";
            var $title2 = "";
            var total = "";
            res.forEach(function (index, value) {
                $title1 = "";
                $title2 = "";

                $title1 += "<div class=\"section-title\">\n                    <h1>" + res[value].sectionTitle + "</h1>\n                </div>\n                <div class=\"section-box-adv\"><a class=\"box-adv-ad\" href=\"" + res[value].sectionHref + "\" target=\"_blank\"><img src=\"" + res[value].sectionImg + "\" alt=\"\"></a></div>";
                // console.log($title1)
                console.log();
                res[value].childArr.forEach(function (index, val) {
                    if (res[value].childArr[val].productSign == "") {
                        $title2 += " <li class=\"iphone\">\n                        <div class=\"section-item-box\">\n                            <a class=\"box-img\" href=\"product.html\" target=\"_blank\" productId=" + res[value].childArr[val].id + ">\n                                <img src=\"" + res[value].childArr[val].imgAddress + "\" alt=\"\">\n                                <span class=\"box-info\">\n                                    <span class=\"goods-name\">" + res[value].childArr[val].goodsName + "</span>\n                                    <span class=\"goods-desc\">" + res[value].childArr[val].goodsDesc + "</span>\n                                    <span class=\"goods-price\">" + res[value].childArr[val].goodsPrice + "</span>\n                                    </span>\n                                    </a>\n                            </div>\n                        </li>";
                    } else {
                        $title2 += " <li class=\"iphone\">\n                        <div class=\"section-item-box\">\n                            <a class=\"box-img\" href=\"product.html\" target=\"_blank\" productId=" + res[value].childArr[val].id + ">\n                                <img src=\"" + res[value].childArr[val].imgAddress + "\" alt=\"\">\n                                <span class=\"box-info\">\n                                    <span class=\"goods-name\">" + res[value].childArr[val].goodsName + "</span>\n                                    <span class=\"goods-desc\">" + res[value].childArr[val].goodsDesc + "</span>\n                                    <span class=\"goods-price\">" + res[value].childArr[val].goodsPrice + "</span>\n                                    </span><span class=\"product-sign\">" + res[value].childArr[val].productSign + "</span>    \n                            </a>\n                            </div>\n                        </li>";
                    }
                });
                // console.log($title2)
                total += "<div class=\"section-box section-phone-box\">\n                        " + $title1 + "\n                        <div class=\"section-box-row\">\n                            <div class=\"container\">\n                                <ul class=\"clearfix\">\n                                " + $title2 + "\n                                </ul>\n                                </div>\n                            </div>\n                        </div>";
                // console.log(total)
            });
            main.html(total);
            var settionBox = $(".section-box");
            // console.log(settionBox)
            settionBox.eq(0).find("li").eq(0).addClass("big");
            settionBox.eq(0).find("li").eq(1).addClass("big");
            settionBox.eq(0).find("li").eq(1).addClass("big2");
            settionBox.eq(0).find("li").eq(5).addClass("last");
            settionBox.eq(0).find("li").eq(9).addClass("last");

            settionBox.eq(1).find("li").eq(3).addClass("last");
            settionBox.eq(1).find("li").eq(7).addClass("last");
            settionBox.eq(1).find("li").eq(11).addClass("last");
            settionBox.eq(1).find("li").eq(0).find("a").addClass("img-retina");
            settionBox.eq(1).find("li").eq(4).find("a").addClass("img-retina");
            settionBox.eq(1).find("li").eq(8).find("a").addClass("img-retina");

            settionBox.eq(2).find("li").eq(3).addClass("last");
            settionBox.eq(2).find("li").eq(7).addClass("last");
            settionBox.eq(2).find("li").eq(0).find("a").addClass("img-retina");
            settionBox.eq(2).find("li").eq(4).find("a").addClass("img-retina");

            settionBox.eq(3).find("li").eq(3).addClass("last");
            settionBox.eq(3).find("li").eq(7).addClass("last");
            settionBox.eq(3).find("li").eq(0).find("a").addClass("img-retina");
            settionBox.eq(3).find("li").eq(4).find("a").addClass("img-retina");
        }
    });
    window.onload = function () {
        var box_img = $(".section-phone-box").find(".box-img");
        box_img.click(function () {
            var goodsName = $(this).find(".goods-name");
            var goodsDesc = $(this).find(".goods-desc");
            var goodsPrice = $(this).find(".goods-price");
            var goodsImg = $(this).find("img").attr("src");
            var goodsId = $(this).attr("productId");

            $.cookie("name", goodsName.html());
            $.cookie("desc", goodsDesc.html());
            $.cookie("price", goodsPrice.html());
            $.cookie("img", goodsImg);
            $.cookie("id", goodsId);
        });
    };
    // 首页结束

});